<!DOCTYPE html>
<html lang="zh-CN">
<head>


    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=0.5, maximum-scale=2.0, user-scalable=yes" />
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="renderer" content="webkit">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>蓝牙测试</title>
    　　<link rel="apple-touch-icon" href="/apple-touch-icon.png（180*180）">
    　　<link rel="icon" href="/favicon.ico（32*32）">
    <!--<script src=”//api.html5media.info/1.1.8/html5media.min.js></script>-->
    <script src="//cdn.jsdelivr.net/npm/eruda"></script>
    <script>eruda.init();</script>

    <script>
        function setNotify(){

            var deviceId = window.hilink.getStorageSync('deviceId');
            var serviceId = window.hilink.getStorageSync('serviceId');
            var characteristicId = window.hilink.getStorageSync('characteristicId');
            window.hilink.notifyBLECharacteristicValueChange(deviceId,serviceId,characteristicId,true);
            window.hilink.onBLECharacteristicValueChange ("ValueChange");
        }
        window.hilink.onBLEConnectionStateChange ("connetState");

        function openBluetoothAdapterJS(){
            var err= window.hilink.openBluetoothAdapter();
            alert("openBluetoothAdapterJS "+err);
        }

        function closeBluetoothAdapterJS(){
            var err= window.hilink.closeBluetoothAdapter();
            alert("closeBluetoothAdapterJS "+err);
        }

        function createBLEConnectionJS(){


            var deviceId = window.hilink.getStorageSync('deviceId');
            var serviceId = window.hilink.getStorageSync('serviceId');
            var characteristicId = window.hilink.getStorageSync('characteristicId');
//            var deviceId = 'C8:FD:19:58:BC:10';
            var retCode = window.hilink.createBLEConnection(deviceId);

            alert("javascript:retCode ="+retCode);
            ;
        }

        function connetState(re)
        {
            var data = eval("("+re+")")
            if(data.connected==true) {
                console.log('连接状态'+data.connected)
                setNotify();
            }else {
                console.log('连接状态'+data.connected)
            }
        }

        function ValueChange(re){
            console.log("valueC:"+re);
        }

        function closeBLEConnectionJS(){
            var retCode = window.hilink.closeBLEConnection('C8:FD:19:58:BC:10');
            alert("javascript:retCode ="+retCode);
        }
        function writeBLECharacteristicValueJS(){
            var deviceId = window.hilink.getStorageSync('deviceId');
            var serviceId = window.hilink.getStorageSync('serviceId');
            var characteristicId = window.hilink.getStorageSync('characteristicId');

            var ByteArrData = window.document.getElementById('ByteArrData')
            var buf ;
            if(ByteArrData.value){
                buf = ByteArrData.value;
            } else {
                var buf = [1,3,0,50,0,30,100,13];
            }alert(buf)
//            var buf = [1,3,0,50,0,30,100,13];

            console.log('deviceId:'+deviceId);
            console.log('serviceId:'+serviceId);
            console.log('characteristicId:'+characteristicId);
            console.log('buf:'+buf);
            var data = JSON.stringify(buf);
            var callback="onCharacterWrite";
            var retCode =window.hilink.writeBLECharacteristicValue(deviceId, serviceId, characteristicId, data,callback);
            alert("javascript:retCode ="+retCode);
        }

        function onCharacterWrite(re){
            alert("aaa"+re);
//            readBLECharacteristicValueJS()
        }


        function readBLECharacteristicValueJS(){
            var deviceId = window.hilink.getStorageSync('deviceId');
            var serviceId = window.hilink.getStorageSync('serviceId');
            var characteristicId = window.hilink.getStorageSync('characteristicId');
            var callback="onCharacterRead"
            var data = window.hilink.readBLECharacteristicValue(deviceId, serviceId, characteristicId,callback);
            alert("javascript:data ="+data);
        }

        function onCharacterRead(re){
            alert("bbb"+re);
        }



    </script>

    <script type="text/javascript">

        //回调函数
        function openBluetoothAdapterCallBack(str){
            alert("javascript:"+str);
        }

        function closeBluetoothAdapterCallBack(str){
            alert("javascript:"+str);
        }

        function stopBluetoothDevicesDiscoveryCallBack(str){
            alert("javascript:"+str);
        }

        //        function onCharacterWrite(re){
        //            alert("aaa"+re);
        //        }

        function getConnectedBluetoothDevicesCall(str){
            alert("aaccc"+str);
        }
    </script>

</head>


<body>
<div style="margin: auto;border: 1px  solid royalblue">
    <h5>设置设备信息</h5>
    <div id="setConfDiv">
        <div style="background-color: white;margin-top: 10px;">
            <input id="deviceId" type="text" value="C8:FD:19:58:BC:10" placeholder="C8:FD:19:58:BC:10"> 设备ID
        </div>
        <div style="background-color: white;margin-top: 10px;">
            <input id="serviceId" value="0000ffe0-0000-1000-8000-00805f9b34fb" placeholder="0000ffe0-0000-1000-8000-00805f9b34fb"> serviceId 服务UUID
        </div>
        <div style="background-color: white;margin-top: 10px;">
            <input id="characteristicId" value="0000ffe1-0000-1000-8000-00805f9b34fb" placeholder="0000ffe1-0000-1000-8000-00805f9b34fb"> characteristicId 特征码UUID
        </div>
    </div>

    <div style="background-color: white;margin-top: 10px;">
        <button id="setConf" onclick="setConf()">设置</button>
    </div>
    <div style="background-color: white;margin-top: 10px;">
        <button id="reConf" onclick="reConf()">重置</button>
    </div>
</div>

<div style="margin-top: 10px;border: 1px  solid royalblue">
    <div style="background-color: white;margin-top: 10px;">
        <button id='btn00' type='submit' onclick='openBluetoothAdapterJS()'>开启蓝牙</button>
    </div>

    <div>
        <div style="background-color: white;margin-top: 10px;">
            <button id='btn01' type='submit' onclick="createBLEConnectionJS()">连接设备</button>
        </div>
        <code>
            var deviceId = "C7:EC:BB:D8:51:3C";<br >
            var retCode = window.hilink.createBLEConnection(deviceId);
        </code>
        <br>
    </div>
    <br>
</div>

<div style="margin-top: 10px;border: 1px  solid royalblue">
    <div style="background-color: white;margin-top: 10px;">
        <button id='btn055' type='submit' onclick='setNotify()'>设置notify</button>
    </div>
    <code>
        function setNotify(){

        var deviceId = window.hilink.getStorageSync('deviceId');
        var serviceId = window.hilink.getStorageSync('serviceId');
        var characteristicId = window.hilink.getStorageSync('characteristicId');
        window.hilink.notifyBLECharacteristicValueChange(deviceId,serviceId,characteristicId,true);
        }
    </code>
</div>

<div style="margin-top: 10px;border: 1px  solid royalblue">
    <div style="background-color: white;margin-top: 10px;">
        <button id='btn03' type='submit' onclick='closeBLEConnectionJS()'>关闭连接</button>
    </div>
    <code>
        var retCode = window.hilink.closeBLEConnection();<br >
        alert("javascript:retCode ="+retCode);
    </code>
</div>

<div style="margin-top: 10px;border: 1px  solid royalblue">
    <div>
        <div style="background-color: white;margin-top: 10px;">
            <input type="text" id="ByteArrData" placeholder="[1,3,0,50,0,30,100,13]">输入byte数组
        </div>
        <div style="background-color: white;margin-top: 10px;">
            <button id='btn001' type='submit' onclick="writeBLECharacteristicValueJS()">向蓝牙设备写数据</button>
        </div>
        <code>
            var deviceId = "C8:FD:19:58:C2:01";<br >
            var serviceId = "0000ffe0-0000-1000-8000-00805f9b34fb";<br >
            var characteristicId = "0000ffe1-0000-1000-8000-00805f9b34fb";<br >

            var buf = [1,3,0,50,0,30,100,13];<br >
            var data = JSON.stringify(buf);<br >
            var callback="onCharacterWrite";<br >
            var retCode =window.hilink.writeBLECharacteristicValue(deviceId, serviceId, characteristicId, data,callback);<br >
            alert("javascript:retCode ="+retCode);<br >

            function onCharacterWrite(re){<br >
            alert("aaa"+re);<br >
            readBLECharacteristicValueJS()<br >
            }<br >
        </code>
    </div>
</div>

<div style="margin-top: 10px;border: 1px  solid royalblue">
    <div>
        <div style="background-color: white;margin-top: 10px;">
            <button id='btn02' type='submit' onclick="readBLECharacteristicValueJS()">读取蓝牙设备数据</button>
        </div>
        <code>
            var deviceId = "C8:FD:19:58:BC:10";<br >
            var serviceId = "0000ffe0-0000-1000-8000-00805f9b34fb";<br >
            var characteristicId = "0000ffe1-0000-1000-8000-00805f9b34fb";<br >
            var callback="onCharacterRead"<br >
            var data = window.hilink.readBLECharacteristicValue(deviceId, serviceId, characteristicId,callback);<br >
            alert("javascript:data ="+data);<br >

            function onCharacterRead(re){<br >
            alert("bbb"+re);<br >
            }<br >
        </code>
    </div>
</div>
<input id="deviceIddef" type="text">
<input id="serviceIddef" type="text" >
<input id="characteristicIddef" >
<script>
    window.onload = function(){

    }
    function setConf(){
        var setConfDiv = window.document.getElementById('setConfDiv');
        window.hilink.setStorageSync('deviceId', window.document.getElementById('deviceId').value)
        window.hilink.setStorageSync('serviceId', window.document.getElementById('serviceId').value)
        window.hilink.setStorageSync('characteristicId', window.document.getElementById('characteristicId').value)

        setConfDiv.innerHTML = '<div style="background-color: white;margin-top: 10px;">' +
                '<br>设备ID:' + window.hilink.getStorageSync('deviceId')+
                '<br>serviceId 服务UUID:'+ window.hilink.getStorageSync('serviceId')+
                '<br>characteristicId 特征码UUID:'+ window.hilink.getStorageSync('characteristicId')+
                '</div>'
    }

    function reConf()
    {
        var setConfDiv = window.document.getElementById('setConfDiv');
        window.hilink.setStorageSync('deviceId', '')
        window.hilink.setStorageSync('serviceId', '')
        window.hilink.setStorageSync('characteristicId','')
        setConfDiv.innerHTML = ' <div style="background-color: white;margin-top: 10px;">'+
                '<input id="deviceId" type="text" placeholder="C8:FD:19:58:C2:01"> 设备ID'+
                '</div>'+
                '<div style="background-color: white;margin-top: 10px;">'+
                '<input id="serviceId" placeholder="0000ffe1-0000-1000-8000-00805f9b34fb"> serviceId 服务UUID'+
                '</div>'+
                '<div style="background-color: white;margin-top: 10px;">'+
                '<input id="characteristicId" placeholder="0000ffe1-0000-1000-8000-00805f9b34fb"> characteristicId 特征码UUID'+
                '</div>'
    }

    window.hilink.setStorageSync = function(key,val){
        window.document.getElementById(key+'def').value = val;
    }

    window.hilink.getStorageSync = function(key){
        return window.document.getElementById(key+'def').value;
    }

</script>

</body>
</html>